package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.mess.ArticleVisitStreamMess;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class HotArticleIncrHandlerListener {

    @Autowired
    ApArticleService apArticleService;

    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC)
    public void onMsg(String msg){
        //{"articleId":1632281198040584194,"collect":0,"comment":0,"like":2,"view":0}
        log.info("消费者收到消息: {}",msg);
        //重新计算文章分值 更新db  更新redis
        if(StringUtils.isNotBlank(msg)){
            ArticleVisitStreamMess mess = JSON.parseObject(msg, ArticleVisitStreamMess.class);
            apArticleService.updateArticleScore(mess);
        }

    }
}
